import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from "@/layout";
import { getToken } from "@/utils/auth"; // get token from cookie
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
//push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch(err => err);
};
//replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalReplace.call(this, location, onResolve, onReject);
  return originalReplace.call(this, location).catch(err => err);
};

const routes = [
  {
    path: "/",
    component: Layout,
    redirect: "/report",
    children: [
      {
        path: "report",
        component: () => import("@/views/report/index"),
        name: "首页",
        meta: { title: "首页" },
      },
      {
        path: "/user",
        component: () => import("@/views/user/index"),
        name: "用户管理",
      },
      {
        path: "/report",
        component: () => import("@/views/report/index"),
        name: "报表管理",
      },
      {
        path: "/authority",
        component: () => import("@/views/authority/index"),
        name: "科室管理",
      },
      {
        path: "/report-create",
        component: () => import("@/views/report/create"),
        name: "科室管理报表",
      },
      {
        path: "/department",
        component: () => import("@/views/department/index"),
        name: "部门管理",
      },
      {
        path: "/leader-weekly-schedule",
        component: () => import("@/views/schedule/leader/index"),
        name: "领导周日程",
      },
      // {
      //   path: "/leader-weekly-schedule-create",
      //   component: () => import("@/views/schedule/leader/create"),
      //   name: "领导周日程创建",
      // },
      {
        path: "/investigator-weekly-schedule",
        component: () => import("@/views/schedule/investigator/index"),
        name: "办案人员周日程",
      },
      {
        path: "/investigator-weekly-schedule-create",
        component: () => import("@/views/schedule/investigator/create"),
        name: "办案人员周日程创建",
      },
    ],
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
  },
  {
    path: '*',
    component: () => import("@/views/exception/404"),
  }
]
export function loadRoutes(routerConfig) {
  const routes = [
    {
      path: '/manage',
      component: Layout,
      children: []
    }
  ];
  routerConfig.forEach((routeData) => {
    const component = () => import(`@/views${routeData.path}/index.vue`);
    const route = {
      path: routeData.path,
      component,
      name: routeData.title,
    };
    routes[0].children.push(route);
  });
  return routes;
}
const router = new VueRouter({
  routes,
});


export default router
